﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Compression;
using ICSharpCode.SharpZipLib;
using System.IO;
using ICSharpCode;
using System.Windows.Forms;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading;
using System.Reflection;

namespace Archives2Go
{
    class Program
    {
        static void Main(string[] args)
        {
            
             string DstFile = @"C:\\temp";


             Assembly _assembly;
          
             _assembly = Assembly.GetExecutingAssembly();
                                        
        
          System.IO.Stream myFile  =  _assembly.GetManifestResourceStream("Archives2Go.Archive2Go.zip");
                 

          System.Resources.ResourceManager rm = new System.Resources.ResourceManager("Resources",System.Reflection.Assembly.GetExecutingAssembly());



  

        

         string[] names = _assembly.GetManifestResourceNames();



             UnZipFiles(myFile,DstFile,null,false);
            Thread.Sleep(2000);
                             
            InstallSoftware();

            //Must have this or install is not finished before creator method wants to run
            Thread.Sleep(20000);

            OpenDoc();
            Thread.Sleep(500);
            StartCreator();
                
                   
        }

     
    
      public static void InstallSoftware()
      {
         ProcessStartInfo si = new ProcessStartInfo(@"c:\\temp\\archive2go\\archive2go.msi\","/qr");
         si.WindowStyle = ProcessWindowStyle.Normal;
         si.UseShellExecute = true;
         Process.Start(si);
      }
        

      public static void StartCreator()
      {
         ProcessStartInfo si = new ProcessStartInfo(@"C:\\Program Files\Advansys\\Archive To Go\\archive2go.exe\","/qr");
         si.WindowStyle = ProcessWindowStyle.Normal;
         si.UseShellExecute = true;
         Process.Start(si);

      }

      public static void OpenDoc()
      {
                ProcessStartInfo si = new ProcessStartInfo(@"c:\\temp\\archive2go\\readme.pdf");
                si.WindowStyle = ProcessWindowStyle.Normal;
                si.UseShellExecute = true;
                Process.Start(si);
      }
               

        public static void UnZipFiles(System.IO.Stream zipPathAndFile, string outputFolder, string password, bool deleteZipFile)
        {
            ICSharpCode.SharpZipLib.Zip.ZipInputStream s = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(zipPathAndFile);
            if (password != null && password != String.Empty)
                s.Password = password;
             ICSharpCode.SharpZipLib.Zip.ZipEntry theEntry;
            string tmpEntry = String.Empty;
            while ((theEntry = s.GetNextEntry()) != null)
            {
                string directoryName = outputFolder;
                string fileName = Path.GetFileName(theEntry.Name);
                // create directory 
                if (directoryName != "")
                {
                    Directory.CreateDirectory(directoryName);
                }
                if (fileName != String.Empty)
                {
                    if (theEntry.Name.IndexOf(".ini") < 0)
                    {
                        string fullPath = directoryName + "\\" + theEntry.Name;
                        fullPath = fullPath.Replace("\\ ", "\\");
                        string fullDirPath = Path.GetDirectoryName(fullPath);
                        if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
                        FileStream streamWriter = File.Create(fullPath);
                        int size = 2048;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }
                        streamWriter.Close();
                    }
                }
            }
            s.Close();
           // if (deleteZipFile)
                //File.Delete(zipPathAndFile);
        }
      }  
  }
    

  